package com.sike.netty.jinjie.eventloop.sub;

import com.sike.netty.jinjie.codec.protocol.message.Message;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j
public class EventLoopGroupTestServer {
    public static void main(String[] args) {
        // 新建一个独立的 EventLoopGroup 专门用来处理长耗时业务
        EventLoopGroup loopGroup01 = new DefaultEventLoopGroup(2);
        EventLoopGroup loopGroup02 = new DefaultEventLoopGroup(2);

        new ServerBootstrap()
                .group(new NioEventLoopGroup(),new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new StringDecoder());
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                log.info("与客户端[{}]建立连接",ctx.channel());
                            }
                        });
                        ch.pipeline().addLast("loopGroup-01",new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.info("loopGroup-01 接收消息：{}", msg);
                                msg = msg + "-loopGroup-01";

                                ctx.fireChannelRead(msg);
                            }
                        });
                        ch.pipeline().addLast(loopGroup01,"loopGroup-02",new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.info("loopGroup-02 接收消息：{}", msg);
                                msg = msg + "-loopGroup-02";

                                ctx.fireChannelRead(msg);
                            }
                        });
                        ch.pipeline().addLast(loopGroup02,"loopGroup-03",new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.info("loopGroup-03 接收消息：{}", msg);
                            }
                        });

                    }
                 }).bind(8081);
    }
}
